package com.houxy.controller;


import com.houxy.repository.mapper.AdminMapper;
import com.houxy.repository.model.bo.ExpertBO;
import com.houxy.repository.model.entity.*;
import com.houxy.repository.model.vo.NoticeVO;
import com.houxy.service.*;
import com.houxy.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminContrller {


    private static final int BUFFER_SIZE = 16 * 1024;
    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ExpertService expertService;

    @Autowired
    private UserService userService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private AdminMapper adminMapper;

    @RequestMapping("/updateAdmin")
    public String updateAdmin(Admin admin) {
        adminMapper.updateByPrimaryKeySelective(admin);
        return "";
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().removeAttribute("currentAdmin");
        return "";
    }

    @RequestMapping("/hospitalManager")
    public ModelAndView hospitalManager() {
        ModelAndView modelAndView = new ModelAndView();
        List<Hospital> hospitalList = hospitalService.findAll();
        modelAndView.addObject("hospitalAdminList", hospitalList);
        modelAndView.setViewName("admin/hospital/managerHospital");
        return modelAndView;
    }

    @RequestMapping("/addHospital")
    public ModelAndView addHospital(Hospital hospital) {
        boolean b = hospitalService.addHospital(hospital);
        ModelAndView modelAndView = new ModelAndView();
        if (b == true) {
            modelAndView.addObject("result", "添加医院成功！");
        } else {
            modelAndView.addObject("result", "添加医院失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/hospitalManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/deleteHospital")
    public ModelAndView deleteHospital(int id) {
        boolean b = hospitalService.deleteHospital(id);
        ModelAndView modelAndView = new ModelAndView();
        if (b == true) {
            modelAndView.addObject("result", "删除医院成功！");
        } else {
            modelAndView.addObject("result", "删除医院失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/hospitalManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/departmentManager")
    public ModelAndView departmentManager() {
        ModelAndView modelAndView = new ModelAndView();
        List<Department> departmentList = departmentService.findAllDepartment();
        modelAndView.addObject("departmentAdminList", departmentList);

        modelAndView.setViewName("admin/department/managerDepartment");
        return modelAndView;
    }

    @RequestMapping("/addDepartment")
    public ModelAndView addDepartment(Department department) {
        ModelAndView modelAndView = new ModelAndView();
        boolean b = departmentService.addDepartment(department);
        if (b == true) {
            modelAndView.addObject("result", "添加科室成功！");
        } else {
            modelAndView.addObject("result", "添加科室失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/departmentManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/delDepartment")
    public ModelAndView delDepartment(int id) {
        boolean b = departmentService.deleteDepartment(id);
        ModelAndView modelAndView = new ModelAndView();
        if (b == true) {
            modelAndView.addObject("result", "删除科室成功！");
        } else {
            modelAndView.addObject("result", "删除科室失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/departmentManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/expertManager")
    public ModelAndView expertManager() {
        ModelAndView modelAndView = new ModelAndView();
        List<ExpertBO> expertAllList = expertService.findAll();
        modelAndView.addObject("expertAllList", expertAllList);

        modelAndView.setViewName("admin/expert/managerExpert");
        return modelAndView;
    }

    @RequestMapping("/addExpert")
    public ModelAndView addExpert(Expert expert) {
        ModelAndView modelAndView = new ModelAndView();
        boolean b = expertService.addExpert(expert);
        if (b == true) {
            modelAndView.addObject("result", "添加医生成功！");
        } else {
            modelAndView.addObject("result", "添加医生失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/expertManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/delExpert")
    public ModelAndView delExpert(int id) {
        boolean b = expertService.deleteExper(id);
        ModelAndView modelAndView = new ModelAndView();
        if (b == true) {
            modelAndView.addObject("result", "删除专家成功！");
        } else {
            modelAndView.addObject("result", "删除专家失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/expertManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/userManager")
    public ModelAndView userinfoManager() {
        ModelAndView modelAndView = new ModelAndView();
        List<User> userList = userService.findAllUser();
        modelAndView.addObject("userList", userList);

        modelAndView.setViewName("admin/user/managerUser");
        return modelAndView;
    }

    @RequestMapping("/addUser")
    public ModelAndView addUser(User user) {
        ModelAndView modelAndView = new ModelAndView();
        String s = userService.registerUser(user);
        if ("注册成功".equals(s)) {
            modelAndView.addObject("result", "添加用户成功！");
        } else {
            modelAndView.addObject("result", "添加用户失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/userManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }


    @RequestMapping("/delUser")
    public ModelAndView delUser(int id) {
        boolean b = userService.deleteUser(id);
        ModelAndView modelAndView = new ModelAndView();
        if (b == true) {
            modelAndView.addObject("result", "删除用户成功！");
        } else {
            modelAndView.addObject("result", "删除用户失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/userManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/noticeManager")
    public ModelAndView noticeManager() {
        ModelAndView modelAndView = new ModelAndView();
        List<Notice> noticeList = noticeService.findAll();
        List<NoticeVO> noticeVOList = new ArrayList<>();
        if (noticeList != null && noticeList.size() > 0) {
            noticeList.stream().forEach(notice -> {
                String createTime = DateUtil.DateToStringYYYYMMDDHHMMSS(notice.getCreateTime());
                NoticeVO noticeVO = new NoticeVO(notice.getId(), notice.getTitle(), notice.getContent(), createTime);
                noticeVOList.add(noticeVO);
            });
        }
        modelAndView.addObject("noticeList", noticeVOList);
        modelAndView.setViewName("admin/notice/managerNotice");
        return modelAndView;
    }

    @RequestMapping("/addNotice")
    public ModelAndView addNotice(Notice notice) {
        ModelAndView modelAndView = new ModelAndView();
        boolean b = noticeService.addNotice(notice);
        if (b == true) {
            modelAndView.addObject("result", "添加公告成功！");
        } else {
            modelAndView.addObject("result", "添加公告失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/noticeManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/delNotic")
    public ModelAndView delNotic(int id) {
        boolean b = noticeService.deleteNotice(id);
        ModelAndView modelAndView = new ModelAndView();
        if (b == true) {
            modelAndView.addObject("result", "删除公告成功！");
        } else {
            modelAndView.addObject("result", "删除公告失败！请联系管理员！");

        }
        modelAndView.addObject("path", "admin/noticeManager.html");
        modelAndView.setViewName("common/succeed");
        return modelAndView;
    }

    @RequestMapping("/detailNotice")
    public ModelAndView detailNotice(int id) {
        ModelAndView modelAndView = new ModelAndView();
        Notice notice = noticeService.findById(id);
        String createTime = DateUtil.DateToStringYYYYMMDDHHMMSS(notice.getCreateTime());
        NoticeVO noticeVO = new NoticeVO(notice.getId(), notice.getTitle(), notice.getContent(), createTime);
        modelAndView.addObject("notice", noticeVO);
        modelAndView.setViewName("admin/notice/detailNotice");
        return modelAndView;
    }

    @RequestMapping("/upload")
    public String upload(MultipartFile file, HttpServletRequest httpServletRequest) throws IOException {
        String newFileName = new Date().getTime() + file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
        String filePath = httpServletRequest.getServletContext().getRealPath("upload") + "//" + newFileName;
        File dstFile = new File(filePath);
        file.transferTo(dstFile);
        httpServletRequest.getSession().setAttribute("newFileName", newFileName);
        httpServletRequest.getSession().setAttribute("file", file);
        httpServletRequest.getSession().setAttribute("filePath", "/upload" + "/" + newFileName);
        return "upload/upload_re";

    }

    @RequestMapping("/system")
    public String system(HttpServletRequest httpServletRequest) {
        List<Hospital> hospitalList = hospitalService.findAll();
        httpServletRequest.getSession().setAttribute("hospitalList", hospitalList);

        //初始化医院
        List<Department> departmentList = departmentService.findAllDepartment();
        httpServletRequest.getSession().setAttribute("departmentList", departmentList);

        //初始化公告
        List<Notice> noticeList = noticeService.findAllLimit();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");

        noticeList.stream().forEach(notice -> {

            notice.setTime(sdf.format(notice.getCreateTime()));
        });
        httpServletRequest.getSession().setAttribute("noticeIndexList", noticeList);
        return "admin/index";
    }
}
